#include "gameobject.h"
#include "spacewargame.h"
//#include "engine.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Background::Background(QString name, QString bgPath) 
	: GObject(name)
{
	m_imgBg.load(bgPath);
	m_dx = m_dy = 0;
}

Background::~Background()
{
}

void Background::SetScroll(double dx, double dy)
{
	prepareGeometryChange();
	m_dx = dx;
	m_dy = dy;	
}

QRectF Background::boundingRect() const
{
	return QRectF(0, 0, m_imgBg.width(), m_imgBg.height());
}

void Background::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(option)
    Q_UNUSED(widget)

	painter->drawImage(0, 0, m_imgBg, m_dx, m_dy, m_imgBg.width()-m_dx, m_imgBg.height()-m_dy);
	painter->drawImage(m_imgBg.width()-m_dx, 0, m_imgBg, 0, 0, m_dx, m_imgBg.height());
	painter->drawImage(0, m_imgBg.height()-m_dy, m_imgBg, 0, 0, m_imgBg.width(), m_dy);

	/*int dx = qAbs((int)m_scroll_x % m_imgBg.width());
	int dy = qAbs((int)m_scroll_y % m_imgBg.height());
	painter->drawImage(0, 0, m_imgBg, dx, dy, m_imgBg.width()-dx, m_imgBg.height()-dy);
	painter->drawImage(m_imgBg.width()-dx, 0, m_imgBg, 0, 0, dx, m_imgBg.height());
	painter->drawImage(0, m_imgBg.height()-dy, m_imgBg, 0, 0, m_imgBg.width(), dy);*/
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Ship::Ship(QString name) : GObject(name)
{
	m_w = 20;
	m_h = 40;
	setTransformOriginPoint(m_w/2, m_h/2);	
}

Ship::~Ship()
{
}

QRectF Ship::boundingRect() const
{
	return QRectF(0,0,m_w,m_h);
}

void Ship::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(option)
    Q_UNUSED(widget)
	
	painter->setBrush(Qt::red);

	QPointF pt[3] = {
		QPointF(m_w/2, 0.0),
		QPointF(0.0, m_h),
		QPointF(m_w, m_h)
	};
	
	painter->drawPolygon(pt, 3);
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Minimap::Minimap(QString name, QSize size) : GObject(name)
{
	//m_pImgMap = new QImage(w, h, QImage::Format_ARGB32);
	m_size = size;
}

Minimap::~Minimap()
{
	//if(m_pImgMap)
	//	delete m_pImgMap;
}

void Minimap::updateMinimap()
{
	prepareGeometryChange();
}

QPoint Minimap::ConvertPoint(const QPoint& pt) const
{
	SpaceWarGame* pGame = static_cast<SpaceWarGame*>(GEngine::GetGame());
	QSize world = pGame->GetWorldSize();
	return QPoint(pt.x()*m_size.width()/world.width(), pt.y()*m_size.height()/world.height());
}

QRect Minimap::GetScreenRectToMinimap(const QRect& rtScreen) const
{
	const QPoint& ptLt = ConvertPoint((const QPoint&)rtScreen.topLeft());
	const QPoint& ptBr = ConvertPoint((const QPoint&)rtScreen.bottomRight());
	return QRect(ptLt, ptBr);
}

QPoint Minimap::GetScreenPosToMinimap(const QPoint& ptGo) const
{
	return QPoint(1,1);
}
	

QRectF Minimap::boundingRect() const
{
	return QRectF(0,0,m_size.width(),m_size.height());
}

void Minimap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	//Q_UNUSED(option)
    //Q_UNUSED(widget)
	QRect mini_world(0,0,m_size.width(),m_size.height());
	painter->setBrush(Qt::black);
	painter->setPen(Qt::white);
	painter->drawRect(mini_world);
	
	SpaceWarGame* pGame = static_cast<SpaceWarGame*>(GEngine::GetGame());
	const QRect& rtScreen = pGame->GetScreenRectToWorld();
	const QRect& mini_screen = GetScreenRectToMinimap(rtScreen);

	//painter->setBrush(Qt::white);
	painter->setBrush(Qt::NoBrush);
	painter->setPen(Qt::yellow);
	painter->drawRect(mini_screen);	

	//const QSize& screen = GEngine::GetGame()->GetWorldPosition();
	
	/*GObjects* gos = GEngine::GetGame()->GetAllObjects();
	GObjects_itor itor;
    for(itor = gos->begin(); itor != gos->end(); itor++)
    {
        GObject* pGo = *itor;
		const QPoint& pt = GetScreenPosToMinimap((const QPoint&)pGo->pos());
		
		QRect rtGo = QRect(pt.x()-2, pt.y()-2, 4, 4);	
		painter->fillRect(rtGo, QBrush(Qt::red));	
    }*/
}
